package com.cennavi.mineplay.user;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping(value = "/user")
public class UserController {

	@Autowired
	protected JdbcTemplate jdbc;
	
	@RequestMapping(value = "/register")
	public Object tableList(String user_id,String username,String password,String email) {
		String sql = "insert into user_info(user_id,username,password,email) values (?,?,?,?)";

		try {
			
			jdbc.update(sql, user_id,username,password,email);
			
			Map<String,String> result = new HashMap<String,String>();
			result.put("result","success");
			
			return result;

		} catch (Exception e) {
			e.printStackTrace();
			
			Map<String,Object> map = new HashMap<String,Object>();
			
			map.put("error_info", e.getMessage());
			
			return map;
		}

	}
	
	@RequestMapping(value = "/log")
	public Object log(String user_id,String password) {
		String sql = "select * from user_info where user_id=? and password=?";

		try {
			
			Map<String,Object> map = jdbc.queryForMap(sql, user_id,password);
			
		 
			
				return map;

		} catch (Exception e) {
			e.printStackTrace();
			
			Map<String,Object> map = new HashMap<String,Object>();
			
			map.put("error_info", e.getMessage());
			
			return map;
		}

		//return null;
	}
	
	@RequestMapping(value = "/works")
	public Object works(long user_id) {
		String sql = "select * from user_works where userid= ?";

		try {
			
			return jdbc.queryForList(sql, user_id);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
	
	
	@RequestMapping(value = "/update_data")
	public Object works(long user_id,String sql) {
		String update_sql = "insert into update_record(user_id,sql,oper_time) values (?,?,now()) ";

		try {
			
			jdbc.execute(sql);
			
			jdbc.update(update_sql, user_id,sql);
			
			Map<String,String> result = new HashMap<String,String>();
			result.put("result","success");
			
			return result;

		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
	
	
}
